#include "imcommon.h"

int write_yahooMsg_Login(MsgNode *node)
{
    /*
    {"sessionId":"pNh6QtTs3B6ulDu7MdyvmCEotxFWwsUfbmzb","primaryLoginId":"xiyingit@yahoo.cn","
    contacts":[{"contact":{"id":"fengjie917","uri":"","groups":[{"group":"Friends","name":"
    Friends","uri":""},{"group":"\u597d\u53cb","name":"\u597d\u53cb","uri":""}]}},{"contact":{
    "id":"lzbworking","authorized":0,"uri":"","groups":[{"group":"\u597d\u53cb","name":"
    \u597d\u53cb","uri":""}]}}],"displayInfo":{"avatarPreference":"0"},"mpopInfo":{"state":0,"
    resourceId":1,"totalEndpoints":1},"notifyServerToken":{"token":"
    2iDQVdvbZW9azhNGKSw2RBsFH7_74RRi6JG7i795Q4yPxvbK1y7kHatw6uOatSg9HROaV1fRwnMWuZF4-|CFSK6pjQbCif_8tllRCPEw--","expires":1346499617},"server":"prod1.rest-core.msg.yahoo.com","notifyServer":"prod2.rest-notify.msg.yahoo.com","constants":{"presenceSubscriptionsMaxPerRequest":500}}
      */
    if (NULL == node->body)
        return -1;
    
    char *tmp    = NULL;

    char *p = NULL;
    p = str_in_node((char **)&node->username, MAX_UN_LEN, node->body, node->real_bodyLen, "primaryLoginId\":\"", "\",\"", 17, 0) ;
    if (NULL == p)
        return -1;
    
    if (!strstr(node->username,"@yahoo.cn"))
        strcat(node->username,"@yahoo.com.cn");
            
    usr_record *puser = search_user(user_record,node->srcMac, "@yahoo");
    if (puser != NULL)
        del_user(puser);
   
    insert_user(user_record,node->srcMac, node->username);

    node->friends = NULL;
    get_friends(&node->friends, p, "contact\":{\"id\":\"", "\",\"", 16, 0, "@yahoo", "@yahoo.com.cn", 13);
    printf("friends:%s\n", node->friends);
    node->msgType = Login;
    output_msg(node);

    free(node->friends);
    node->friends = NULL;

    return 0;
}

int write_yahooMsg_Send(MsgNode *node)
{
     /*
    {"convId":"xiyingit@yahoo.cn~fengjie917~yahoo",
    "txnId":"xiyingit@yahoo.cn~1345015274718~3",
    "message":"<font face=\"Arial\" size=\"10\">hahahahaha</font>","sendAs":"xiyingit@yahoo.cn"}
      */
    char *pstart = NULL;
    char *pend = NULL;
   // printf("data:%s\n",node->data);
   
    pstart = strstr(node->data,"{\"convId\":\"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart,"~");
    if (NULL == pend)
        return -1;
    strncpy(node->from,pstart,pend-pstart);
    node->from[pend-pstart+1] = '\0';
    if (!strstr(node->from,"@yahoo.cn"))
        strcat(node->from,"@yahoo.com.cn");
    strcpy(node->username,node->from);


    pend += 1;    
    pstart = strstr(pend,"~yahoo");
    if (NULL == pstart)
        return -1;
    strncpy(node->to,pend,pstart-pend);
    node->to[pstart-pend+1] = '\0';
    if (!strstr(node->to,"@"))
        strcat(node->to,"@yahoo.com.cn");

    pend = strstr(pstart,"message\":\"");
    if (NULL == pend)
        return -1;
    pend += 10;
    pstart= strstr(pend,"\",\"sendAs");
    if (NULL == pstart)
        return -1;
    node->content = (char *)malloc(pstart-pend+1);
    memset(node->content,0,pstart-pend+1);
    strncpy(node->content,pend,pstart-pend);
    node->content[pstart-pend+1] = '\0';
    char *str_tmp = clear_html_tag(node->content);
    clear_tag(str_tmp);
    free(node->content);
    node->content = clear_msgcontent_symbol(str_tmp);
    str_tmp = NULL;
   // printf("node->content:%s\n",node->content);
  /*
    int result = yahooMsg_str_convert(node->content, pend-pstart);
    if (result == -1)
	return -1;
	*/

    node->msgType = Text;
    node->s_or_r = 0;
    output_msg(node);
    memset(node->username, 0, MAX_UN_LEN);
    return 0;
}

int write_yahooMsg_Rec(MsgNode *node)
{
    /*
    { "@mpopState" : 0, "@pendingMsg" : 0, "@syncStatus" : 0, "responses" : [ { "message" : { "status" : 1, "sequence" : 12, "
    sender" : "fengjie917" , "receiver" : "xiyingit@yahoo.cn" , "msg" : "<font face=\"Arial\" size=\"10\">gaergrag <\/font>" , "
    timeStamp" : 1345092963, "hash" : "YW5R4nORaVBrKz70Z4alJW27t+2sbw==" , "
    msgContext" : "YW5R4nORaVBrKz70Z4alJW27t+2sbw=="  } }
 ] }
      */     
    char *pstart     = NULL;
    char *pend      = NULL;
    //char *strtmp = NULL;
    //printf("data:%s\n",node->data);
    //char patternrecive[] = "\"message\" : (.*?)  } }\n";
  //  regcompile_2(node->data, patternrecive, &strtmp);
    pstart = strstr(node->data, "\"message\"");
    if (NULL == pstart)
        return -1;
    
    pstart = strstr(pstart, "sender\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    strncpy(node->from,pstart,pend-pstart);
    node->from[pend-pstart+1] = '\0';
    if (!strstr(node->from,"@yahoo.cn"))
        strcat(node->from,"@yahoo.com.cn");
    
    pstart = strstr(pend,"receiver\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 13;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    strncpy(node->to,pstart,pend-pstart);
    node->to[pend-pstart+1] = '\0';
    if (!strstr(node->to,"@yahoo.cn"))
        strcat(node->to,"@yahoo.com.cn");

    strcpy(node->username,node->to);

    pstart = strstr(pend,"msg\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 8;
    pend = strstr(pstart,"\" , \"");
    if (NULL == pend)
        return -1;
    node->content = (char *)malloc(pend-pstart+2);
    memset(node->content,0,pend-pstart+1);
    strncpy(node->content,pstart,pend-pstart);
    node->content[pend-pstart+1] = '\0';
    char *tmp_content = clear_html_tag(node->content);
    OUT_PUT;
    free(node->content);
    node->content = clear_msgcontent_symbol(tmp_content);
    
    node->s_or_r = 1;
    output_msg(node);
   // free(strtmp);
    free(tmp_content);
    tmp_content = NULL;
    return 0;
}

int write_yahooMsg_Logout(MsgNode *node)
{
    /*{ "@mpopState" : 0, "@pendingMsg" : 0, "@syncStatus" : 0, "responses" : [ { "logOff" : { "status" : 1, "sequence" : 5, "
    buddy" : "xiyingit@yahoo.cn"  } }
    ] }
      */
    node->msgType = Logout;

    char *pstart = NULL;
    char *pend  = NULL;

    pstart = strstr(node->data, "\"buddy\" : \"");
    if (NULL == pstart)
        return -1;
    pstart += 11;
    pend = strstr(pstart, "\"  }");
    strncpy(node->username,pstart,pend-pstart);
    node->username[pend-pstart+1] = '\0';

    printf("%s exited\n",node->username);
    struct tm *ptm = localtime(&(node->time));
    printf("exited time is:%u-%u-%u %u:%02u\n", ptm->tm_year+1900, ptm->tm_mon+1, ptm->tm_mday,ptm->tm_hour, ptm->tm_min);
    
    output_msg(node);
    return 0;
}

int write_yahooMsg_Logout2(MsgNode * node)
{
    char *pstart = NULL;
    char *pend  = NULL;

    if (NULL==node->body && NULL==node->data)
        return -1;

    node->msgType = Logout;
    
    usr_record *p = search_user(user_record,node->srcMac, "@yahoo");
    if (NULL != p)
    {
        memcpy(node->username,p->username, strlen(p->username)+1);
        del_user(p);
    }
    output_msg(node);
    
    return 0;
}

